package dshop.action.admin;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Db;

import appbase.jfinal.ext.ApiBaseController;
import appbase.utils.DateUtil;
import dshop.interceptor.ApiAuthInterceptor;
import dshop.interceptor.ApiReadBodyParamInterceptor;
import dshop.model.StoreSetting;
import dshop.model.activity.ActivityComplain;
import dshop.model.procurement.Procurement;
import dshop.service.RedisService;

@Before({ ApiReadBodyParamInterceptor.class, ApiAuthInterceptor.class })
public class SystemConfigAction extends ApiBaseController {

	public void orderConfig() {
		StoreSetting findFirst = new StoreSetting().findFirst("select * from store_setting");
		setAttr("orderConfig", findFirst);
		renderSuccess();
	}

	public void modifyOrderConfig() {
		new StoreSetting().setAttrsFrom(this, "id,auto_erify_time,out_time").update();
		renderSuccess();
	}

	public void complainList() {
		String phone = getPara("phone", "").trim();
		Date startDate = getParaToDateTime("startDate", "yyyy-MM-dd");
		Date endDate = getParaToDateTime("endDate", "yyyy-MM-dd");
		int type = getParaToInt("type", -1);
		String select = "SELECT t.*,a.phone,CONCAT(a.last_name,a.first_name) username ";
		String from = " FROM ( ";
		List<Object> params = new ArrayList<Object>();
		String where = " where 1=1 ";
		switch (type) {
		case 0:
			from += " SELECT id,account_id,operator,operator_time,handling_opinions,is_handled,activity_id contentId,`explain`,theme,DATE_FORMAT(add_time,'%Y-%m-%d') 'date_time', 0 complain_type,'活动投诉' complain_type_text FROM activity_complain ";
			break;
		case 1:
			from += " SELECT id,account_id,operator,operator_time,handling_opinions,is_handled,procurement_id contentId,`explain`,theme,DATE_FORMAT(date_time,'%Y-%m-%d') 'date_time',1 complain_type,'采购投诉'  complain_type_text FROM bbs_procurement_complain ";
			break;
		case 2:
			from += " SELECT id,account_id,operator,operator_time,handling_opinions,is_handled,member_id contentId,`explain`,theme,DATE_FORMAT(date_time,'%Y-%m-%d') 'date_time',2 complain_type,'会员投诉'  complain_type_text FROM member_complain ";
			break;
		case 3:
			from += " SELECT id,account_id,operator,operator_time,handling_opinions,is_handled,project_id contentId,`explain`,theme,DATE_FORMAT(date_time,'%Y-%m-%d') 'date_time',3 complain_type,'项目合作投诉'  complain_type_text FROM bbs_project_complain ";
			break;
		case 4:
			from += " SELECT id,account_id,operator,operator_time,handling_opinions,is_handled,job_id contentId,`explain`,theme,DATE_FORMAT(date_time,'%Y-%m-%d') 'date_time',4 complain_type,'招聘投诉'  complain_type_text FROM bbs_job_complain ";
			break;
		default:
			from += " SELECT id,account_id,operator,operator_time,handling_opinions,is_handled,activity_id contentId,`explain`,theme,DATE_FORMAT(add_time,'%Y-%m-%d') 'date_time', 0 complain_type,'活动投诉' complain_type_text FROM activity_complain "
					+ " UNION ALL SELECT id,account_id,operator,operator_time,handling_opinions,is_handled,procurement_id contentId,`explain`,theme,DATE_FORMAT(date_time,'%Y-%m-%d') 'date_time', 1 complain_type,'采购投诉'  FROM bbs_procurement_complain "
					+ " UNION ALL SELECT id,account_id,operator,operator_time,handling_opinions,is_handled,member_id contentId,`explain`,theme,DATE_FORMAT(date_time,'%Y-%m-%d') 'date_time', 2 complain_type,'会员投诉' FROM member_complain "
					+ " UNION ALL SELECT id,account_id,operator,operator_time,handling_opinions,is_handled,project_id contentId,`explain`,theme,DATE_FORMAT(date_time,'%Y-%m-%d') 'date_time', 3 complain_type,'项目合作投诉' FROM bbs_project_complain "
					+ " UNION ALL SELECT id,account_id,operator,operator_time,handling_opinions,is_handled,job_id contentId,`explain`,theme,DATE_FORMAT(date_time,'%Y-%m-%d') 'date_time', 4 complain_type,'招聘投诉' FROM bbs_job_complain ";
			break;
		}
		from += " ) t LEFT JOIN member_general_info a on t.account_id = a.account_id ";
		if (!phone.isEmpty()) {
			where += " and a.phone = ? ";
			params.add(phone);
		}
		if (startDate != null) {
			Calendar cal = DateUtil.setTimeToZero(startDate);
			where += " and  t.date_time >= ? ";
			params.add(cal.getTime());
		}
		if (endDate != null) {
			Calendar cal = DateUtil.setTimeToZero(endDate);
			where += " and  t.date_time <= ? ";
			params.add(cal.getTime());
		}
		where += "order by t.date_time desc ";
		page(select, from + where, params.toArray());
		renderSuccess();
	}

	public void markProcessed() {
		Integer complainType = getParaToInt("complainType");
		Integer id = getParaToInt("id");
		String remark = getPara("remark");
		if (complainType != null) {
			String userPhone = RedisService.getApiUserPhone(getParaToInt(ApiBaseController.PARAM_API_KEY_USER_ID));
			switch (complainType) {
			case 0:
				Db.update("update activity_complain set operator = ? , is_handled = 1,operator_time = now(),handling_opinions = ?  where id = ? ",userPhone, remark, id);
				break;
			case 1:
				Db.update("update bbs_procurement_complain set operator = ? , is_handled = 1,operator_time = now(),handling_opinions = ? where id = ? ",userPhone, remark, id);
				break;
			case 2:
				Db.update("update member_complain set operator = ? , is_handled = 1,operator_time = now(),handling_opinions = ? where id = ? ",userPhone, remark, id);
				break;
			case 3:
				Db.update("update bbs_project_complain set operator = ? , is_handled = 1,operator_time = now(),handling_opinions = ? where id = ? ",userPhone, remark, id);
				break;
			case 4:
				Db.update("update bbs_job_complain set operator = ? , is_handled = 1,operator_time = now(),handling_opinions = ? where id = ? ",userPhone, remark, id);
				break;
			}
		}

		renderSuccess();
	}
}
